package switchviz.graphic.gauge.bargraph;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JComboBox;

import org.jdom.Element;

import switchviz.core.ResourceManager;
import switchviz.graphic.IGraphicsPanel;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.JMaskedTextField;

public class BarGraphPanel extends IGraphicsPanel implements ActionListener
{
	/**
	 * 
	 */
	public static final int REP_LINEAR = 0;
	public static final int REP_LOG = 1;
	private static final String CBREPCOMMAND = "selrep";
	private static final long serialVersionUID = 691870682828135016L;
	private BarGraphPersistence _xmlhandler = null;
	private JLabeldComponent _txtname = null;
	private JLabeldComponent _cbmultiport = null;
	private JMaskedTextField _txthistory = null;
	private JMaskedTextField _txtcurrval = null;
	private JMaskedTextField _txtlinewidth = null;
	private JMaskedTextField _txtdatamax = null;
	private JMaskedTextField _txtdatamin = null;
	private JMaskedTextField _txtdatadepth = null;
	private JLabeldComponent _cbdatatype = null;
	
	public BarGraphPanel(Element root)
	{
		super();
		initialize();
		
		setElement(root);
	}
	
	private void initialize()
	{
		_txtname = new JLabeldComponent("Name: ", JLabeldComponent.TEXTFIELD);
		_cbmultiport = new JLabeldComponent("Multiple per Port: ", JLabeldComponent.CHECKBOX);
		_txthistory = new JMaskedTextField("History Memory: ", JMaskedTextField.FLOATNUM);
		_txtcurrval = new JMaskedTextField("Value Bar Height: ", JMaskedTextField.FLOATNUM);
		_txtlinewidth = new JMaskedTextField("Line Width: ", JMaskedTextField.FLOATNUM);
		_txtdatamax = new JMaskedTextField("Max Value: ", JMaskedTextField.FLOATNUM);
		_txtdatamin = new JMaskedTextField("Min Value: ", JMaskedTextField.FLOATNUM);
		_txtdatadepth = new JMaskedTextField("Depth View: ", JMaskedTextField.FLOATNUM);
		_cbdatatype = new JLabeldComponent("Representation: ", JLabeldComponent.COMBOBOX);
		_cbdatatype.getComboBox().insertItemAt("linear", REP_LINEAR);
		_cbdatatype.getComboBox().insertItemAt("locarithmical", REP_LOG);
		_cbdatatype.getComboBox().setActionCommand(CBREPCOMMAND);
		_cbdatatype.getComboBox().addActionListener(this);

		this.add(_txtname);
		this.add(_cbmultiport);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_txthistory);
		this.add(_txtcurrval);
		this.add(_txtlinewidth);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_txtdatamax);
		this.add(_txtdatamin);
		this.add(_txtdatadepth);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_cbdatatype);
	}
	
	public Element getElement()
	{
		BarGraphDataModel datamodel = new BarGraphDataModel();
		datamodel.setDataMax(Float.valueOf(_txtdatamax.getText()));
		datamodel.setDataMin(Float.valueOf(_txtdatamin.getText()));
		datamodel.setDataDepth(Float.valueOf(_txtdatadepth.getText()));
		datamodel.setCurrvalbar(Float.valueOf(_txtcurrval.getText()));
		datamodel.setHistorylength(Integer.valueOf(_txthistory.getText()));
		datamodel.setLinewidth(Float.valueOf(_txtlinewidth.getText()));
		datamodel.setName(_txtname.getText());
		datamodel.setMultiplePerPort(_cbmultiport.getCheckBox().isSelected());
		datamodel.setType(this.getName());
		datamodel.setRepresentation(_cbdatatype.getComboBox().getSelectedIndex());
		return _xmlhandler.getElement(datamodel);
	}
	
	public void setElement(Element node)
	{
		_xmlhandler = new BarGraphPersistence(node);
		
		BarGraphDataModel datamodel = _xmlhandler.getDataModel(node);
		_txthistory.setText(String.valueOf(datamodel.getHistorylength()));
		_txtcurrval.setText(String.valueOf(datamodel.getCurrvalbar()));
		_txtlinewidth.setText(String.valueOf(datamodel.getLinewidth()));
		_txtname.setText(datamodel.getName());
		_cbmultiport.getCheckBox().setSelected(datamodel.isMultiplePerPort());
		_cbdatatype.getComboBox().setSelectedIndex(datamodel.getRepresentation());
		_txtdatamax.setText(String.valueOf(datamodel.getDataMax()));
		_txtdatamin.setText(String.valueOf(datamodel.getDataMin()));
		_txtdatadepth.setText(String.valueOf(datamodel.getDataDepth()));
		this.setName(datamodel.getType());
	}

	public void actionPerformed(ActionEvent evt) 
	{
		if(CBREPCOMMAND.equals(evt.getActionCommand()))
		{
			if(REP_LOG == ((JComboBox)evt.getSource()).getSelectedIndex())
			{
				if(Float.valueOf(_txtdatamin.getText()) < 1.0f)
					_txtdatamin.setText(String.valueOf(1.0f));
			}
		}
	}
	
}
